package com.smfx.yxt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.StudentFruits;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.model.StudentSchoolPrize;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.ScoreSummaryQueryVO;
import com.smfx.yxt.reqVo.StudentFruitsReqVO;
import com.smfx.yxt.service.IScoreSummaryService;
import com.smfx.yxt.service.IStudentFruitsService;
import com.smfx.yxt.service.IStudentSchoolPrizeService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.utils.CommonUtil;
import com.smfx.yxt.utils.DateUtil;
import com.smfx.yxt.view.StudentFruitsView;
import com.smfx.yxt.view.StudentInfoView;
import com.smfx.yxt.vo.StudentFruitsQueryVO;

@Controller
public class StudentGroupupController extends BaseController {

	@Autowired
	private IStudentFruitsService studentFruitsService;
	@Autowired
	private IScoreSummaryService scoreSummaryService;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IStudentSchoolPrizeService studentSchoolPrizeService;
	
	private Logger logger = LoggerFactory.getLogger(StudentGroupupController.class);
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.STUDENT_GROUPUP + "/{studentId}")
	public String studentIndex(@PathVariable Integer studentId, Model model) {
		model.addAttribute("studentId", studentId);
		return "studentGroupup/studentIndex";
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.STUDENT_GROUPUP + "/scoreReport/{examId}")
	public String studentScoreReportForTeacher(Model model, @PathVariable Integer examId) {
		String studentId = request.getParameter("sid");
		StudentInfo student = studentService.selectById(Integer.valueOf(studentId));
		if(student == null) return null;
		model.addAttribute("student", BeanUtils.copyAs(student, StudentInfoView.class));
		model.addAttribute("examId", examId);
		return "studentGroupup/scoreReport";
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.STUDENT_GROUPUP)
	public String studentIndexForParam(Model model) {
		model.addAttribute("studentId", request.getParameter("sid"));
		return "studentGroupup/studentIndex";
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.STUDENT_GROUPUP + "/studentScore")
	public String studentScore(Model model) {
		model.addAttribute("studentId", request.getParameter("sid"));
		return "studentGroupup/studentScore";
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.STUDENT_GROUPUP + "/scoreReport/{examId}")
	public String studentScoreReport(Model model, @PathVariable Integer examId) {
		StudentInfo student = new StudentInfo();
		student.setId(appUser().getParent().getStudentId());
		student.setName(appUser().getParent().getStudentName());
		model.addAttribute("student", BeanUtils.copyAs(student, StudentInfoView.class));
		model.addAttribute("examId", examId);
		return "studentGroupup/scoreReport";
	}
	
	@RequestMapping(MenuConst.STUDENT_GROUPUP + "/studentScore/query")
	@ResponseBody Object studentScoreQuery(ScoreSummaryQueryVO vo){
		StudentInfo studentInfo = studentService.selectById(vo.getStudentId());
		int grade = studentInfo.getGrade().intValue();
		List<Integer> list = new ArrayList<>();
		list.add(grade);
		vo.setScoreList(list);
		return scoreSummaryService.selectStudentScore(vo);
	}
	
	@RequestMapping(MenuConst.STUDENT_GROUPUP + "/studentFruit/page/query")
	@ResponseBody Object studentFruitsPageQuery(StudentFruitsQueryVO vo) {
		try {
			Page<StudentFruits> page = new Page<StudentFruits>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("获奖时间", "desc"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<StudentFruits> list = studentFruitsService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---studentFruitsPageQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.STUDENT_GROUPUP + "/studentFruit/detail/{id}")
	public String studentFruitDetail(Model model, @PathVariable Integer id) {
		StudentFruitsView detail = studentFruitsService.selectDetailById(id);
		model.addAttribute("detail", detail);
		return "studentGroupup/stuFruDetail";
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.STUDENT_GROUPUP + "/studentFruit/detail/{id}")
	public String studentFruitDetailForParent(Model model, @PathVariable Integer id) {
		StudentFruitsView detail = studentFruitsService.selectDetailById(id);
		model.addAttribute("detail", detail);
		return "studentGroupup/stuFruDetail";
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.STUDENT_GROUPUP + "/studentFruit/add/{studentId}")
	public String studentFruitAddPage(Model model, @PathVariable Integer studentId) {
		StudentInfo student = studentService.selectById(studentId);
		model.addAttribute("student", student);
		return "studentGroupup/addStuFru";
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.STUDENT_GROUPUP + "/studentFruit/submit")
	@ResponseBody Object studentFruitSubmit(StudentFruitsReqVO vo) {
		try {
			vo.setTermId(getCurrentTerm().getId());
			StudentInfo studentInfo = studentService.selectById(vo.getStudentId());
			vo.setGradeName(CommonUtil.getGradeName(studentInfo.getGrade()));
			vo.setAwardTime(DateUtil.strToDate(vo.getAwardDate(), DateUtil.DEFAULT_DATE_FORMAT3));
			studentFruitsService.addStudentFruit(vo);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("----studentFruitSubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.STUDENT_GROUPUP + "/teaFruParam")
	@ResponseBody Object teaFruParam() {
		return studentFruitsService.getParamMap();
	}
	
	@RequestMapping(MenuConst.STUDENT_GROUPUP + "/prizeList")
	@ResponseBody Object studentPrizeList(ScoreSummaryQueryVO vo) {
		try {
			List<StudentFruits> fruits = studentFruitsService.selectListByParam(vo);
			List<StudentSchoolPrize> prizes = studentSchoolPrizeService.selectListByParam(vo);
			Map<String, Object> map = new HashMap<>();
			map.put("fruits", fruits);
			map.put("prizes", prizes);
			return map;
		} catch (Exception e) {
			logger.error("---studentPrizeList error={}", e);
		}
		return null;
	}	
}
